import 'package:card_swiper/card_swiper.dart';
import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/clubs/clubs_controller.dart';
import 'package:flutter_changdian_football/pages/clubs/widget/clubs_info_widget.dart';
import 'package:flutter_changdian_football/pages/clubs/widget/dynamic_widget.dart';
import 'package:flutter_changdian_football/pages/clubs/widget/game_record_widget.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/widget/dialog/switch_club_dialog.dart';
import 'package:flutter_changdian_football/widget/gradient_tab_indicator.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/sheet/share_icon_sheet.dart';
import 'package:get/get.dart';

/// 俱乐部
class ClubsOldPage extends StatelessWidget {
  const ClubsOldPage({super.key});

  // @override
  // Widget build(BuildContext context) {
  //   return Scaffold(
  //     body: GetBuilder<ClubsController>(
  //       init: ClubsController(),
  //       builder: (clubsController) {
  //         return Stack(
  //           children: [
  //             Container(
  //               width: double.infinity,
  //               height: double.infinity,
  //               decoration: BoxDecoration(
  //                 gradient: LinearGradient(
  //                   begin: Alignment.topCenter,
  //                   end: Alignment.bottomCenter,
  //                   stops: const [0, 0.3, 1.0],
  //                   colors: [
  //                     const Color(0xff49935E).withOpacity(0.99),
  //                     const Color(0xff4EAE6B).withOpacity(0.35),
  //                     const Color(0xff518361).withOpacity(0),
  //                   ]
  //                 )
  //               ),
  //               child: Column(
  //                 children: [
  //                   Expanded(
  //                     child: SingleChildScrollView(
  //                       physics: const BouncingScrollPhysics(),
  //                       child: SafeArea(
  //                         child: Column(
  //                           children: [
  //                             SizedBox(height: 10.w,),

  //                             Container(
  //                               alignment: Alignment.centerRight,
  //                               margin: EdgeInsets.only(right: 17.w),
  //                               child: SizedBox(
  //                                 width: 16.w,
  //                                 height: 16.w,
  //                                 child: Image.asset('assets/images/ddd.png', fit: BoxFit.fill,),
  //                               ),
  //                             ),

  //                             SizedBox(height: 36.w,),

  //                             ClubsInfoWidget(),

  //                             SizedBox(height: 18.w,),

  //                             _ribbonWidget(clubsController),

  //                             SizedBox(height: 22.w,),

  //                             _otherTeamEmpty(),

  //                             SizedBox(height: 26.w,),

  //                             _tabbarTabWidget(clubsController),

  //                             Container(
  //                               margin: EdgeInsets.only(left: 17.w, right: 17.w),
  //                               padding: EdgeInsets.only(bottom: 10.w),
  //                               decoration: BoxDecoration(
  //                                 borderRadius: BorderRadius.only(bottomLeft: Radius.circular(12.r), bottomRight: Radius.circular(12.r)),
  //                                 color: Colors.white,
  //                               ),
  //                               // height: 300.w,
  //                               height: MediaQuery.of(context).size.height - 100.w - kToolbarHeight,
  //                               child: TabBarView(
  //                                 controller: clubsController.tabController,
  //                                 children: [
  //                                   Container(),
  //                                   Container(),
  //                                 ],
  //                               ),
  //                             ),
  //                           ],
  //                         ),
  //                       ),
  //                     ),
  //                   )
  //                 ],
  //               ),
  //             ),
  //           ],
  //         );
  //       }
  //     ),
  //   );
  // }





  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GetBuilder<ClubsController>(
        init: ClubsController(),
        builder: (clubsController) {
          return Stack(
            children: [
              Container(
                width: double.infinity,
                height: double.infinity,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    stops: const [0, 0.3, 1.0],
                    colors: [
                      const Color(0xff49935E).withOpacity(0.99),
                      const Color(0xff4EAE6B).withOpacity(0.35),
                      const Color(0xff518361).withOpacity(0),
                    ]
                  )
                ),
                child: Column(
                  children: [
                    Expanded(
                      child: ExtendedNestedScrollView(
                        physics: const BouncingScrollPhysics(),
                        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                          return [
                            SliverToBoxAdapter(
                              child: SafeArea(
                                child: Column(
                                  children: [
                                    SizedBox(height: 10.w,),
                              
                                    Container(
                                      alignment: Alignment.centerRight,
                                      margin: EdgeInsets.only(right: 17.w),
                                      child: GestureDetector(
                                        behavior: HitTestBehavior.translucent,
                                        onTap: () {
                                          _showMenu(clubsController);
                                        },
                                        child: SizedBox(
                                          width: 16.w,
                                          height: 16.w,
                                          child: Image.asset('assets/images/ddd.png', fit: BoxFit.fill,),
                                        ),
                                      ),
                                    ),
                              
                                    SizedBox(height: 36.w,),
                              
                                    ClubsInfoWidget(),
                              
                                    SizedBox(height: 18.w,),
                              
                                    _ribbonWidget(clubsController),
                              
                                    SizedBox(height: 22.w,),
                              
                                    // _otherTeamEmpty(),
                                    _otherTeamWidget()
                                  ],
                                ),
                              ),
                            ),
                          ];
                        }, 
                        pinnedHeaderSliverHeightBuilder: () {
                          return kToolbarHeight - 60.w;
                        },
                        onlyOneScrollInBody: true,
                        body: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(height: 26.w,),

                              _tabbarTabWidget(clubsController),

                              Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(left: 17.w, right: 17.w),
                                  padding: EdgeInsets.only(bottom: 10.w),
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.only(bottomLeft: Radius.circular(12.r), bottomRight: Radius.circular(12.r)),
                                    color: Colors.white,
                                  ),
                                  child: TabBarView(
                                    controller: clubsController.tabController,
                                    children: [
                                      DynamicWidget(),
                                      GameRecordWidget(),
                                    ],
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ],
          );
        }
      ),
    );
  }

  // 功能区
  _ribbonWidget(ClubsController clubsController) {
    return SizedBox(
      child: GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 9,
          mainAxisSpacing: 9,
          childAspectRatio: 2.3
        ),
        itemCount: clubsController.gnqList.length,
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        padding: EdgeInsets.symmetric(horizontal: 17.w),
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              if (index == 0) {
                /// 俱乐部信息
                Get.toNamed(AppRoutes.clubInfo);
              } else if (index == 1) {
                /// 财政
                Get.toNamed(AppRoutes.finances);
              } else if (index == 2) {
                /// 创建比赛
                Get.toNamed(AppRoutes.createContest);
              } else if (index == 3) {
                /// 球员
                Get.toNamed(AppRoutes.clubPlayers);
              }
            },
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12.r)
              ),
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 32.w,
                    height: 32.w,
                    child: Image.asset(clubsController.gnqList[index]['icon']),
                  ),
          
                  SizedBox(width: 8.w,),
          
                  SizedBox(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SizedBox(
                          child: Text(clubsController.gnqList[index]['name'], style: TextStyle(fontSize: 14.sp, color: const Color(0xff000000), fontWeight: FontWeight.w500),),
                        ),
                        SizedBox(height: 4.w,),
                        SizedBox(
                          child: Text(clubsController.gnqList[index]['desc'], style: TextStyle(fontSize: 11.sp, color: const Color(0xff999999)),),
                        )
                      ],
                    ),
                  )
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  // 暂无其他团队
  _otherTeamEmpty() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 17.w),
      width: double.infinity,
      height: 168.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.r),
        boxShadow: const [
          BoxShadow(
            color: Color.fromRGBO(138, 211, 161, 0.2),
            offset: Offset(0, 4),
            blurRadius: 12,
            spreadRadius: 0,
          ),
        ],
      ),
    );
  }

  _otherTeamWidget() {
    return SizedBox(
      height: 168.w,
      // child: AnimatedPageViewScrolling(
      //   heightMainPageView: 168.w,
      //   heightItem: 168.w,
      //   viewportFraction: 0.8,
      // ),
      child: Swiper(
        index: 0,
        loop: false,
        // viewportFraction: list.length == 1 ? 1.0 : 0.9,
        viewportFraction: 0.9,
        // scale: list.length == 1 ? 1.0 : 0.97,
        scale: 0.95,
        itemCount: 3,
        itemBuilder: (context, index) {
          return Stack(
            clipBehavior: Clip.none,
            children: [
              Container(
                padding: EdgeInsets.only(right: 10.w),
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.r),
                    color: Colors.red,
                  ),
                ),
              ),

              Positioned(
                right: 1.w,
                top: 16.w,
                child: Container(
                  width: 103.w,
                  height: 40.w,
                  padding: EdgeInsets.only(top: 3.w),
                  decoration: const BoxDecoration(
                    image: DecorationImage(
                      fit: BoxFit.fill, 
                      image: AssetImage('assets/images/game_gray.png')
                    ),
                  ),
                  child: RichText(
                    textAlign: TextAlign.center,
                    text: TextSpan(text: "已报名",style: TextStyle(color: Colors.white, fontSize: 12.sp),
                      children: [
                        TextSpan(text: " 1",style: TextStyle(color:(Colors.white), fontWeight: FontWeight.w600, fontSize: 16.sp),),
                        TextSpan(text: "/10",style: TextStyle(color: Colors.white, fontSize: 12.sp),),
                      ]
                    )
                  )
                )
              ),
            ],
          );
        },
        // pagination: const SwiperPagination(
        //   alignment: Alignment.bottomCenter,
        //   margin: EdgeInsets.all(4),
        //   builder: SwiperPointPagination(),
        // ),
        onTap: (index) {
        },
      ),
    );
  }

  _tabbarTabWidget(ClubsController clubsController) {
    return Stack(
      children: [
        Container(
          margin: EdgeInsets.only(left: 17.w, right: 17.w),
          width: double.infinity,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.r), 
              topRight: Radius.circular(12.r)
            ),
            gradient: const LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Color(0xffBAE5C7), 
                Colors.white
              ]
            ),
          ),
          child: TabBar(
            labelColor: const Color(0xff222222),
            controller: clubsController.tabController,
            isScrollable: true,
            indicatorSize: TabBarIndicatorSize.label,
            indicator: GradientTabIndicator(),
            labelPadding: EdgeInsets.symmetric(horizontal: 15.w),
            indicatorPadding: EdgeInsets.symmetric(vertical: 5.w),
            labelStyle: TextStyle(color: const Color(0xff222222), fontSize: 16.sp, fontWeight: FontWeight.w500),
            unselectedLabelStyle: TextStyle(color: const Color(0xff999999), fontSize: 16.sp, fontWeight: FontWeight.w500),
            tabs: const [
              Tab(text: '动态'),
              Tab(text: '比赛记录')
            ],
          ),
        ),
      ],
    );
  }

  _showMenu(ClubsController clubsController) {
    showMenu(
      color: Colors.white, //弹出框的颜色
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(6.r)
        )
      ), //给弹出框添加一个圆角形状
      elevation: 10,
      context: Get.context!,
      position: const RelativeRect.fromLTRB(100, 50, 0, 0),
      items: <PopupMenuEntry>[
        _popupMenuItem(text: '消息盒子', icon: 'assets/images/xxhz.png', onTap: () {
          Get.toNamed(AppRoutes.clubNews, parameters: {
            'clubId': '${clubsController.clubCenterModel?.clubId}'
          });
        }),
        const PopupMenuDivider(),
        _popupMenuItem(text: '切换俱乐部', icon: 'assets/images/qhjlb.png', onTap: () {
          SwitchClubDialog.show(
            Get.context!,
            createTap: () {
              Get.offNamed(AppRoutes.createClub);
            }
          );
        }),
        const PopupMenuDivider(), 
        _popupMenuItem(text: '显示名设置', icon: 'assets/images/xsmsz.png', onTap: () {
          
        }),
        const PopupMenuDivider(), 
        _popupMenuItem(text: '分享', icon: 'assets/images/fx.png', onTap: () {
          ShareIconSheet.shareIcon('title', 'description', null, 'url');
        }),
      ]
    );
  }

  _popupMenuItem({
    String icon = '',
    String text = '',
    Function? onTap
  }) {
    return PopupMenuItem(
      padding: const EdgeInsets.symmetric(horizontal: 10,),
      height: 24.w,
      child: SizedBox(
        width: 86,
        height: 15,
        child: Row(
          children: [
            Image.asset(icon),
            Padding(padding: EdgeInsets.only(left: 8.w)),
            Text(text, style: TextStyle(color: const Color(0xff333333), fontSize: 11.sp),),
            // if ((_clubViewModel.clubCenterEntity?.checkMessageNum ?? 0) > 0) ...[
            //   const SizedBox(width: 4,),
            //   Container(
            //     width: 15,
            //     height: 15,
            //     decoration: BoxDecoration(
            //       color: const Color(0xffF4333C),
            //       borderRadius: BorderRadius.circular(15)
            //     ),
            //     alignment: Alignment.center,
            //     child: Text('${_clubViewModel.clubCenterEntity?.checkMessageNum ?? 0}', style: const TextStyle(fontSize: 9, color: Colors.white),),
            //   )
            // ]
          ],
        ),
      ),
      onTap: () {
        Future.delayed(const Duration(milliseconds: 100), (){
          onTap?.call();
        });
      },
    );
  }

}